package com.lanxin.oa.zc.service.serviceImpl;

import com.lanxin.bean.ZcAsset;
import com.lanxin.bean.ZcAssetmoveapply;
import com.lanxin.bean.ZcAssetmovedetails;
import com.lanxin.bean.ZcAssetmovelog;
import com.lanxin.oa.zc.dao.ZcYiDongDao;
import com.lanxin.oa.zc.service.ZcYiDongService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class ZcYiDongServiceImpl implements ZcYiDongService {
    @Autowired
    ZcYiDongDao dao;

    @Override
    public List<ZcAssetmoveapply> selectzcyd(Integer empid) {
        return dao.selectydzc(empid);
    }

    @Override
    public List selecttiaojian(String startDate, String endDate, Integer empid, Integer status) {
        return dao.selecttiaojian(startDate, endDate, empid, status);
    }

    @Override
    public List selectflow() {
        return dao.selectflow();
    }

    @Override
    public List selectEmpByempName() {
        return dao.selectEmpByempName();
    }

    @Override
    public List selectJcbaseByDd() {
        return dao.selectJcbaseByDd();
    }

    @Override
    public String createFlowId() {
        Integer tadayFlowCount = dao.getZcYdInfoCount();
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        StringBuilder taday = new StringBuilder(format.format(new Date()));
        StringBuilder prefix = new StringBuilder("ZCYD");

        if (tadayFlowCount == 0) {
            prefix.append(taday).append("01");
        } else {
            if (tadayFlowCount < 10) {
                prefix.append(taday).append("0").append(tadayFlowCount + 1);
            } else {
                prefix.append(taday).append(tadayFlowCount + 1);
            }
        }

        return prefix.toString();
    }

    @Override
    public List selectZcaset() {
        return dao.selectZcaset();
    }

    @Override
    public List selectJcbaseByYdYy() {
        return dao.selectJcbaseByYdYy();
    }

    @Override
    public List selectDeptById() {
        return dao.selectDeptById();
    }

    @Override
    public List selectemp_dept(Integer deptid) {
        return dao.selectemp_dept(deptid);
    }

    @Override
    public ZcAsset selectZcasetkg(Integer oid) {
        return dao.selectZcasetkg(oid);
    }

    @Override
    public void insert(ZcAssetmoveapply zcAssetmoveapply, Integer[] assetId, Integer[] targetPlace, String[] remark) {
        String dh = this.createFlowId();
        zcAssetmoveapply.setDocNo(dh);

        if (assetId != null) {
            for (int i = 0; i < assetId.length; i++) {
                ZcAssetmovedetails zcmx = new ZcAssetmovedetails();
                ZcAsset zcAsset = dao.selectZcasetkg(assetId[i]);
                zcmx.setFormPlace(zcAsset.getDepositary());
                zcmx.setYuanRen(zcAsset.getKeeper());
                zcmx.setAssetId(assetId[i]);
                zcmx.setTargetPlace(targetPlace[i]);
                zcmx.setMoveDate(new Date());
                zcmx.setZcAssetmoveapply(zcAssetmoveapply);
                System.out.println(zcAssetmoveapply.getOid());
                zcmx.setParentid(zcAssetmoveapply.getOid());
                zcmx.setRemark(remark[i]);
                zcAssetmoveapply.getZcydmx().add(zcmx);

            }
        }
        //创建日期
        try {
            zcAssetmoveapply.setCreateDate(new Date());

        } catch (Exception e) {
            e.printStackTrace();
        }
        zcAssetmoveapply.setStatus("0");
        dao.add(zcAssetmoveapply);


    }

    @Override
    public void tijiao(Integer oid, Integer status) {
        ZcAssetmoveapply zcyd = dao.selectbyid(oid);
        zcyd.setSubmitDate(new Date());
        if (status.equals(0)) {
            zcyd.setStatus("3");
        }
        dao.update(zcyd);


    }

    @Override
    public void tobanli(Integer oid, Integer status) {
        ZcAssetmoveapply zcyd = dao.selectbyid(oid);
        zcyd.setMoveDate(new Date());
        if (status.equals(2)) {
            zcyd.setStatus("4");
        }


        Set<ZcAssetmovedetails> ydmx= zcyd.getZcydmx();
        if(ydmx!=null){
            for (ZcAssetmovedetails mx:ydmx){
                System.out.println("添加移动记录楼");
                ZcAssetmovelog zclog =new ZcAssetmovelog();
                zclog.setAssetId(mx.getAssetId());
                zclog.setFormerPlace(mx.getFormPlace());
                zclog.setTargetPlace(mx.getTargetPlace());
                zclog.setZerenren(zcyd.getProposer());
                zclog.setRemark(mx.getRemark());
                zclog.setHandler(zcyd.getCreator());
                zclog.setMoveDate(mx.getMoveDate());
                zclog.setMoveReason(zcyd.getMoveReason());
                dao.add(zclog);
            }

        }
        dao.update(zcyd);
    }

    @Override
    public ZcAssetmoveapply selectbyid(Integer oid) {
        return dao.selectbyid(oid);
    }

    @Override
    public void update(Integer[] assetId, Integer[] targetPlace, String[] remark, ZcAssetmoveapply yd, Integer ydoid[]) {
        ZcAssetmoveapply assetmoveapply = dao.selectbyid(yd.getOid());
        assetmoveapply.getZcydmx().clear();
        if (assetId != null) {
            Set<ZcAssetmovedetails> assetmovedetails = new HashSet<>();
            for (int i = 0; i < assetId.length; i++) {
                ZcAssetmovedetails zcmx = new ZcAssetmovedetails();
                if (ydoid != null && ydoid.length > 0) {
                    if (i < ydoid.length) {
                        zcmx.setOid(ydoid[i]);
                    }
                }
                ZcAsset zcAsset = dao.selectZcasetkg(assetId[i]);
                zcmx.setFormPlace(zcAsset.getDepositary());
                zcmx.setYuanRen(zcAsset.getKeeper());
                zcmx.setAssetId(assetId[i]);
                zcmx.setTargetPlace(targetPlace[i]);
                zcmx.setMoveDate(new Date());
                zcmx.setParentid(yd.getOid());
                zcmx.setRemark(remark[i]);
                assetmovedetails.add(zcmx);
                assetmoveapply.getZcydmx().add(zcmx);
            }
            yd.setZcydmx(assetmovedetails);
        }
        dao.update(assetmoveapply);
    }

    @Override
    public boolean delete(ZcAssetmoveapply assetmoveapply) {
        List<ZcAssetmovedetails> mxlist= dao.selectmx(assetmoveapply.getOid());
        if (mxlist!= null && mxlist.size() > 0) {
            return false;
        }
              dao.delete(assetmoveapply);
              return  true;

    }

}